/**
 * 路由配置文件
 * 定义应用的路由规则和页面组件映射
 */
import { createRouter, createWebHistory } from 'vue-router'

// 路由懒加载
const Layout = () => import('../views/layout/Layout.vue')
const Login = () => import('../views/login/Login.vue')
const Dashboard = () => import('../views/dashboard/Dashboard.vue')
const StudentList = () => import('../views/student/StudentList.vue')
const StudentForm = () => import('../views/student/StudentForm.vue')
const NotFound = () => import('../views/error/NotFound.vue')

/**
 * 路由配置
 * 定义应用的所有页面路径
 */
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { title: '登录', requiresAuth: false }
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    meta: { requiresAuth: false },
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: Dashboard,
        meta: { title: '首页', icon: 'HomeFilled' }
      },
      {
        path: 'student',
        name: 'StudentManagement',
        component: { render: () => import('vue').h('router-view') },
        meta: { title: '学生管理', icon: 'User' },
        children: [
          {
            path: 'list',
            name: 'StudentList',
            component: StudentList,
            meta: { title: '学生列表', icon: 'List' }
          },
          {
            path: 'add',
            name: 'AddStudent',
            component: StudentForm,
            meta: { title: '添加学生', icon: 'Plus' }
          },
          {
            path: 'edit/:id',
            name: 'EditStudent',
            component: StudentForm,
            meta: { title: '编辑学生', icon: 'Edit', hidden: true }
          }
        ]
      }
    ]
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'NotFound',
    component: NotFound,
    meta: { title: '404', hidden: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

/**
 * 全局前置守卫
 * 进行路由拦截，处理登录验证和权限控制
 */
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 学生管理系统` : '学生管理系统'
  
  // 临时禁用登录验证，直接放行所有请求
  next()
  
  /* 原登录验证逻辑，暂时注释
  // 获取token
  const token = localStorage.getItem('token')
  
  // 如果访问的是需要认证的页面，但没有token，则重定向到登录页
  if (to.meta.requiresAuth && !token) {
    next({ path: '/login', query: { redirect: to.fullPath } })
  } else {
    next()
  }
  */
})

export default router 